<html>
<head><meta charset="utf-8"><title>Tackling `undocumented_unsafe_blocks` rust-clippy#7464 · clippy · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/index.html">clippy</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Tackling.20.60undocumented_unsafe_blocks.60.20rust-clippy.237464.html">Tackling `undocumented_unsafe_blocks` rust-clippy#7464</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="248607223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Tackling%20%60undocumented_unsafe_blocks%60%20rust-clippy%237464/near/248607223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Tackling.20.60undocumented_unsafe_blocks.60.20rust-clippy.237464.html#248607223">(Aug 06 2021 at 13:02)</a>:</h4>
<p>I'm currently trying to see how an <code>undocumented_unsafe_blocks</code> lint could be implemented in clippy. AFAICT there are two possible solutions:</p>
<ul>
<li>use plain comments (<code>// SAFETY: ...</code>): this is the most natural approach, and is currently used in the standard library. However since comments are not included in the AST, this would require some hacks to get them. The way I'd do it is getting the source code from the <code>SourceMap</code> and lexing it with <code>rustc_lexer</code>.</li>
<li>use doc comments (<code>/// SAFETY: ...</code>): this would probably be more surprising, but it can lead to some interesting ideas. Doc comments are kept in the AST (though discarded in the HIR), so no hacks would be required. The main problem which probably kills the idea is that attributes on expressions are experimental (<a href="https://github.com/rust-lang/rust/issues/15701">#15701</a>).</li>
</ul>
<p>Would the hacks required for using plain comments be OK for use in clippy? Are there any other solutions I missed?</p>



<a name="248612387"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Tackling%20%60undocumented_unsafe_blocks%60%20rust-clippy%237464/near/248612387" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Tackling.20.60undocumented_unsafe_blocks.60.20rust-clippy.237464.html#248612387">(Aug 06 2021 at 13:44)</a>:</h4>
<p>We have some lints which snip the code using <code>Span</code>s and then make a decision based on the returned <code>String</code>. However, all of them are Allow-by-default. Adding this in a similar fashion would be okay IMO, but probably also require it to be Allow-by-default. I would wait for some more feedback before diving into this.</p>
<p>Another possibility would be to also put the lint behind the same feature gate as the "doc comments on statement feature". <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="248614921"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Tackling%20%60undocumented_unsafe_blocks%60%20rust-clippy%237464/near/248614921" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Tackling.20.60undocumented_unsafe_blocks.60.20rust-clippy.237464.html#248614921">(Aug 06 2021 at 14:03)</a>:</h4>
<p>Discussed this with <span class="user-mention" data-user-id="369415">@xFrednet</span> and we noted that only attributes on expressions <em>inside statements</em> are experimental. So the following is accepted on stable:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="sd">/// SAFETY: ...</span>
<span class="k">unsafe</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<p>But this is not:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"></span>
<span class="w">    </span><span class="sd">/// SAFETY: ...</span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* some reaaaaaaaaaaly long expression that causes line wrap */</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>But most interestingly:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="sd">/// SAFETY: ...</span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="sd">/// SAFETY: ...</span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
</code></pre></div>
<p><em>is</em> accepted.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>